package com.yzs.exam.utils;

import com.yzs.exam.common.exception.BusinessException;
import com.yzs.exam.entity.Question;
import com.yzs.exam.entity.enums.QuestionTypeSuggestTimeEnum;
import com.yzs.exam.entity.exam.Rule;
import org.springframework.validation.BindException;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yzs
 * @create 2021-03-27 15:15
 */
public class ExamUtil {
    /**
     * 打分
     * @param score
     * @return
     */
    public static String scoreToVM(Integer score) {
        if (score % 10 == 0) {
            return String.valueOf(score / 10);
        } else {
            return String.format("%.1f", score / 10.0);
        }
    }

    /**
     *
     * @param score
     * @return
     */
    public static Integer scoreFromVM(String score) {
        if (score == null) {
            return null;
        } else {
            return (int) (Float.parseFloat(score) * 10);
        }
    }

    //日期格式装换
    public static String secondToVM(Integer second) {
        String dateTimes;
        long days = second / (60 * 60 * 24);
        long hours = (second % (60 * 60 * 24)) / (60 * 60);
        long minutes = (second % (60 * 60)) / 60;
        long seconds = second % 60;
        if (days > 0) {
            dateTimes = days + "天 " + hours + "时 " + minutes + "分 " + seconds + "秒";
        } else if (hours > 0) {
            dateTimes = hours + "时 " + minutes + "分 " + seconds + "秒";
        } else if (minutes > 0) {
            dateTimes = minutes + "分 " + seconds + "秒";
        } else {
            dateTimes = seconds + " 秒";
        }
        return dateTimes;
    }

    private static final String ANSWER_SPLIT = ",";

    public static String contentToString(List<String> contentArray) { // 将数组排序后进行拼接以逗号隔开
        return contentArray.stream().sorted().collect(Collectors.joining(ANSWER_SPLIT));
    }


    public static List<String> contentToArray(String contentArray) {
        return Arrays.asList(contentArray.split(ANSWER_SPLIT));
    }

    private static final String FORM_ANSWER_SPLIT = "_";

    public static Integer lastNum(String str) {
        Integer start = str.lastIndexOf(FORM_ANSWER_SPLIT);
        return Integer.parseInt(str.substring(start + 1));
    }

    public static Integer getExamPaperSuggestTime(List<Question> list){
        int suggestTime = 0;
        for (Question question: list){
            int type = question.getQuestionType();
            switch (type){
                case 1:
                    suggestTime += QuestionTypeSuggestTimeEnum.SingleChoice.getSuggestTime();
                    break;
                case 2:
                    suggestTime += QuestionTypeSuggestTimeEnum.MultipleChoice.getSuggestTime();
                    break;
                case 3:
                    suggestTime += QuestionTypeSuggestTimeEnum.TrueFalse.getSuggestTime();
                    break;
                case 4:
                    suggestTime += QuestionTypeSuggestTimeEnum.GapFilling.getSuggestTime();
                    break;
                default:
                    suggestTime += QuestionTypeSuggestTimeEnum.ShortAnswer.getSuggestTime();
            }
        }
        return suggestTime;
    }

    /**
     * 随机生成试卷题目id列表（包括错题和新题）
     * @param errorNum
     * @param newNum
     * @param errorIds
     * @param newIds
     * @return
     */
    public static List<Integer> randomNewErrorQuestionIds(Integer errorNum, Integer newNum, List<Integer> errorIds, List<Integer> newIds){
        ArrayList<Integer> ids = new ArrayList<>(errorNum + newNum);
        if (errorIds.size() == 0){
            throw new BusinessException("没有错题历史，请调整");
        }
        if(errorIds.size() < errorNum || newIds.size() < newNum){
            throw new BusinessException("题库不足，请调整");
        }
        Random r = new Random();
        HashSet<Integer> set = new HashSet<>();//不能存储重复的值
        if (!IsZeroSize(errorIds) && errorNum > 0){
            for (int i = 0; i < errorNum; ){
                int rant = r.nextInt(errorIds.size());
                if(!set.contains(rant)){
                    set.add(rant);
                    ids.add(errorIds.get(rant));
                    i++;
                }
            }
        }
        set.clear();
        if (!IsZeroSize(newIds) && newNum > 0){
            for (int i = 0; i < newNum;){
                int rant = r.nextInt(newIds.size());
                if (!set.contains(rant)){
                    set.add(rant);
                    ids.add(newIds.get(rant));
                    i++;
                }
            }
        }
        return ids;
    }

    private static <T> boolean IsZeroSize(List<T> list){
        return list.size() == 0;
    }

    /**
     * 计算试卷约束总分
     * @param rule
     * @return
     */
    public static Integer getExpectTotalScore(Rule rule){
        int total =
                rule.getChoiceNum() / 3 * 2  + rule.getChoiceNum() / 3 * 1 + rule.getChoiceNum() / 3 * 4 + rule.getFillingNum() * 2 + rule.getAnswerNum() * 5;
        return total;
    }
}
